<template>
  <view class="login">
    <button @click="wxLogin">获取信息</button>
    <button @click="payment">微信支付</button>
    <button @click="getAuth">获取授权</button>
    <button @click="getUserProfile">获取getUserProfile</button>
    <button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
      getPhone
    </button>
    <button @click="sendMockReq">模拟请求</button>
    <button @click="createOrder">创建订单</button>
  </view>
</template>

<script setup>
import { fetchOpenId, sendCreateOrder } from "./../../utils/api/test";
const wxLogin = () => {
  console.log("获取信息");
  uni.getUserInfo({
    desc: "登录数据",
    success: (res) => {
      console.log(res);
      uni.login({
        success(lres) {
          console.log(lres);
        },
      });
      uni.showModal({
        title: "提示",
        content: "登录绑定",
        success: function (res) {
          if (res.confirm) {
            console.log("用户点击确定");
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
  });
};

const payment = () => {
  //订单对象，从服务器获取
  //   var orderInfo = {
  //     appid: "wx499********7c70e", // 应用ID（AppID）
  //     partnerid: "148*****52", // 商户号（PartnerID）
  //     prepayid: "wx202254********************fbe90000", // 预支付交易会话ID
  //     package: "Sign=WXPay", // 固定值
  //     noncestr: "c5sEwbaNPiXAF3iv", // 随机字符串
  //     timestamp: "1597935292", // 时间戳（单位：秒）
  //     sign: "A842B45937F6EFF60DEC7A2EAA52D5A0", // 签名，这里用的 MD5 签名
  //   };

  // {"success":true,"code":200,"msg":"success","data":{"appId":"wxce0adcc05cb37dfe","nonceStr":"Xaj5VD57DsLKC4BeEYfWfd7ml0ivhaRQ","packageVal":"prepay_id=wx20104141625639b7e0027439cfd5830000","signType":"RSA","paySign":"a9lSFuzU5IN8HxaBy7Vs3QFE2mrez/nSfQXyxxHJxBU7gCb9OCn22foLbbrRLel7r+IilBZo7qS1wacN3rebC06lY7OehhZcRQNOp715txkmotYy8I7ppNHTHqxj5vZ8KSAcXPg+r0h8oHArGmP19oOQfnHU3PVtGGl22bVfNFz/jyJAGfUPFRrk6LlNzIHuQ83FnXp16TvcTCjcwGbmrnHxNp77keYg4O0+UGKFTaIse2KRexo0WUVCjhVaZ8AZI0Wb/FBeDKzLANcq5Kh/r3glIJ4jR/PqZQQsZ+qY+dFkcE7XqN+Yfb6ZfSj6pg+/Bqv6pAU+kW+zc318IBF5Rg==","timeStamp":"1740019302"}}
  const obj = {
    success: true,
    code: 200,
    msg: "success",
    data: {
      appId: "wxce0adcc05cb37dfe",
      nonceStr: "Xaj5VD57DsLKC4BeEYfWfd7ml0ivhaRQ",
      packageVal: "prepay_id=wx20104141625639b7e0027439cfd5830000",
      signType: "RSA",
      paySign:
        "a9lSFuzU5IN8HxaBy7Vs3QFE2mrez/nSfQXyxxHJxBU7gCb9OCn22foLbbrRLel7r+IilBZo7qS1wacN3rebC06lY7OehhZcRQNOp715txkmotYy8I7ppNHTHqxj5vZ8KSAcXPg+r0h8oHArGmP19oOQfnHU3PVtGGl22bVfNFz/jyJAGfUPFRrk6LlNzIHuQ83FnXp16TvcTCjcwGbmrnHxNp77keYg4O0+UGKFTaIse2KRexo0WUVCjhVaZ8AZI0Wb/FBeDKzLANcq5Kh/r3glIJ4jR/PqZQQsZ+qY+dFkcE7XqN+Yfb6ZfSj6pg+/Bqv6pAU+kW+zc318IBF5Rg==",
      timeStamp: "1740019302",
    },
  };
  uni.requestPayment({
    provider: "wxpay",
    timeStamp: obj.data.timeStamp,
    nonceStr: obj.data.nonceStr,
    package: obj.data.packageVal,
    signType: "RSA",
    paySign: obj.data.paySign,

    success: function (res) {
      console.log("success:" + JSON.stringify(res));
    },
    fail: function (err) {
      console.log("fail:" + JSON.stringify(err));
    },
  });
  //   uni.getProvider({
  //     service: "payment",
  //     success: function (res) {
  //       console.log(res.provider);
  //       if (~res.provider.indexOf("wxpay")) {
  //       }
  //     },
  //   });
};
const getAuth = () => {
  uni.authorize({
    scope: "scope.userLocation",
    success() {
      uni.getLocation();
    },
  });
  //   uni.openAppAuthorizeSetting({
  //     success: function () {
  //       console.log("授权成功");
  //     },
  //     fail: function () {
  //       console.log("授权失败");
  //     },
  //   });
};
const getPhoneNumber = (e) => {
  console.log(e.detail.code); // 动态令牌
  console.log(e.detail.errMsg); // 回调信息（成功失败都会返回）
  console.log(e.detail.errno); // 错误码（失败时返回）
};
const sendMockReq = () => {
  console.log("发送模拟请求");
  fetchOpenId("0a1v7Sll2wjj4f4VnJml2wOmah0v7SlQ").then((res) => {
    console.log("res:", res);
  });
};
const createOrder = () => {
  console.log("创建订单");

  const payload = {
    description: "第5件商品描述",
    wxOpenId: "oB4hW7PM3f2KTnuBPA02Ubojwez0",
    outTradeNo: "cssptsl2",
    totalFee: 1, // 单位：分
  };
  sendCreateOrder(payload).then((res) => {
    console.log("res:", res);
    if (res.data.code == 200) {
      console.log("res.data.data:", res.data.data);
      uni.requestPayment({
        provider: "wxpay",
        signType: "RSA",
        package: res.data.data.packageVal,
        ...res.data.data,

        success: function (res) {
          console.log("success:" + JSON.stringify(res));
        },
        fail: function (err) {
          console.log("fail:" + JSON.stringify(err));
        },
      });
    }
    if (res.data.code == 500) {
      uni.showToast({
        title: "订单创建失败! " + res.data.msg,
        icon: "none",
      });
    }
  });
};
const getUserProfile = () => {
  uni.getUserProfile({
    desc: "用于完善会员信息",
    lang: "zh_CN",
    success(res) {
      console.log(res);
    },
  });
};
</script>

<style></style>
